package com.withoutwords.engine;

import java.io.Serializable;

import java.util.Arrays;

import java.util.List;


import org.json.JSONException;
import org.json.JSONObject;

public class Round implements ConvertableJson,Serializable {

	String roundId = "";
	RoundMediaType mediaType;
	String mediaName = "";
	RoundMediaTime mediaTime;
	
	String[] words;
	public String[] getWords() {
		return words;
	}
	boolean[] wordsState;
	
	public RoundMediaType getMediaType() {
		return mediaType;
	}
	public void setMediaType(RoundMediaType mediaType) {
		this.mediaType = mediaType;
	}
	public String getMediaName() {
		return mediaName;
	}
	public void setMediaName(String mediaName) {
		this.mediaName = mediaName;
		
		words = mediaName.split(" ");
		wordsState = new boolean[words.length];
		Arrays.fill(wordsState, false);
	}
	public void approveWord(int index)
	{
		if(index == -1)
		{
			Arrays.fill(wordsState, true);
		}
		if(index >= 0 && index < wordsState.length)
			wordsState[index] = true;
	}
	
	public boolean isRoundSolved()
	{
		boolean solved  = wordsState[0];
		for(int i = 1 ; i< wordsState.length ; i++)
		{
			solved &= wordsState[i];
		}
		return solved;
	}
	public RoundMediaTime getMediaTime() {
		return mediaTime;
	}
	public void setMediaTime(RoundMediaTime mediaTime) {
		this.mediaTime = mediaTime;
	}
	@Override
	public JSONObject toJSON() {
		JSONObject obj = new JSONObject();
		try {
			obj.put("roundId", roundId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			if(mediaType != null)
			{
			obj.put("mediaType", mediaType.ordinal());
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(mediaTime != null)
			{
			obj.put("mediaTime", mediaTime.ordinal());
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			obj.put("mediaName", mediaName);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			String solved = "";
			for(int i =0 ; i< wordsState.length ; i++)
			{
				if(wordsState[0])
				{
					solved += i+",'";
				}
			}
			//to remove the last comma
			solved = solved.substring(0, solved.length()-2);
			obj.put("solved", solved);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return obj;	}
	@Override
	public boolean fromJSON(JSONObject obj) {
		try {
			roundId = obj.getString("roundId");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			setMediaName( obj.getString("mediaName"));
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			mediaTime = RoundMediaTime.values()[ obj.getInt("mediaTime")];
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			mediaType = RoundMediaType.values()[ obj.getInt("mediaType")];
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			String solved = obj.getString("solved");
			String[] solvedIndices = solved.split(",");
			
			for(int i=0 ; i< solvedIndices.length ; i++)
			{
				int ind = new Integer(solvedIndices[i]);
				wordsState[ind] = true;
			}
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
		
	}

	
}
